package com.yeyks.controller.other;

import com.alibaba.fastjson.JSON;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.account.dal.domain.SubAccountOperationRecord;
import com.yeyks.account.service.SubAccountOperationRecordService;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.CashOutHotelSelectForHotelPageParam;
import com.yeyks.common.param.CashOutHotelSelectForManagePageParam;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.commonReference.service.hotel.HotelInfoService;
import com.yeyks.hotel.dal.domain.CashOutHotel;
import com.yeyks.hotel.dal.domain.HotelInfo;
import com.yeyks.hotel.service.CashOutHotelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@Api(tags = "酒店提现接口信息", description = "酒店提现接口信息")
@RestController
@RequestMapping("/ajax/cashOutHotel")
public class CashOutHotelController {

    @Autowired
    private CashOutHotelService cashOutHotelService;
    @Autowired
    private AccountInfoService accountService;
    @Autowired
    private HotelInfoService hotelService;
    @Autowired
    private SubAccountOperationRecordService subAccountOperationRecordService;

    @ApiOperation("提现")
    @PostMapping("/cashWithdrawalOperation")
    public AjaxResponseBody cashWithdrawalOperation(String token, Integer amount) {
        if (StringUtils.isBlank(token)) {
            throw new ServiceException("token不存在");
        }
        //account信息
        AccountInfo accountInfo = null;
        try {
            accountInfo = accountService.token2AccountInfo(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null == accountInfo) {
            throw new ServiceException("用户信息错误");
        }
        //如果是子账户就使用父账户的id进行
        Integer accountId = accountInfo.getId();
        if (null != accountInfo.getManagerId()) {
            accountId = accountInfo.getManagerId();
        }
        List<HotelInfo> hotelInfos = hotelService.searchHotelsByOperator(accountId);
        if (null == hotelInfos || hotelInfos.size() < 1) {
            throw new ServiceException("用户信息错误");
        }
        HotelInfo hotelInfo = hotelInfos.get(0);
        AjaxResponseBody ajaxResponseBody = cashOutHotelService.cashWithdrawalOperation(hotelInfo, amount);
        //记录子账号操作
        if (0 == ajaxResponseBody.getCode()) {
            CashOutHotel cashOutHotel = (CashOutHotel) ajaxResponseBody.getBody();
            SubAccountOperationRecord subAccountOperationRecord = new SubAccountOperationRecord();
            subAccountOperationRecord.setOperator(accountInfo.getId());
            subAccountOperationRecord.setType(1);
            subAccountOperationRecord.setBusinessId(cashOutHotel.getId());
            HashMap<String, Object> map = new HashMap<>();
            map.put("token", token);
            map.put("amount", amount);
            subAccountOperationRecord.setParameterJson(JSON.toJSONString(map));
            subAccountOperationRecord.setMessage("子账号提现" + (amount / 100d) + "元");
            subAccountOperationRecordService.save(subAccountOperationRecord);
        }
        return ajaxResponseBody;
    }

    @ApiOperation("页面列表查询(管理页面)")
    @PostMapping("/selectForManagePage")
    public AjaxResponseBody selectForManagePage(CashOutHotelSelectForManagePageParam cashOutHotelSelectForManagePageParam) {
        return cashOutHotelService.selectForManagePage(cashOutHotelSelectForManagePageParam);
    }

    @ApiOperation("页面列表查询(酒店页面)")
    @PostMapping("/selectForHotelPage")
    public AjaxResponseBody selectForHotelPage(CashOutHotelSelectForHotelPageParam cashOutHotelSelectForHotelPageParam) {
        String token = cashOutHotelSelectForHotelPageParam.getToken();
        if (StringUtils.isBlank(token)) {
            throw new ServiceException("token不存在");
        }
        //account信息
        AccountInfo accountInfo = null;
        try {
            accountInfo = accountService.token2AccountInfo(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null == accountInfo) {
            throw new ServiceException("用户信息错误");
        }
        //如果是子账户就使用父账户的id进行
        Integer accountId = accountInfo.getId();
        if (null != accountInfo.getManagerId()) {
            accountId = accountInfo.getManagerId();
        }
        List<HotelInfo> hotelInfos = hotelService.searchHotelsByOperator(accountId);
        if (null == hotelInfos || hotelInfos.size() < 1) {
            throw new ServiceException("用户信息错误");
        }
        HotelInfo hotelInfo = hotelInfos.get(0);
        cashOutHotelSelectForHotelPageParam.setHotelId(hotelInfo.getId());
        return cashOutHotelService.selectForHotelPage(cashOutHotelSelectForHotelPageParam);
    }

    @ApiOperation("修改状态,1待审核(申请中)，2审核通过(已打款)，3 审核未通过(打款失败)，4 失败")
    @PostMapping("/updateStatus")
    public AjaxResponseBody updateStatus(Integer id, Integer status, String remark) {
        return cashOutHotelService.updateStatus(id, status, remark);
    }

}
